Skip to content

Conversation

natanrolnik
Copy link
Contributor

While working on a sample project, I found it was really hard to parse the response of the SCAN and KEYS commands.

After talking to @adam-fowler, he told me about the RESPTokenDecodable protocol, which makes it really easy to implement type safe responses, so I added them.

@codecov-commenter
Copy link

codecov-commenter commented Oct 10, 2025

Codecov Report

❌ Patch coverage is 0% with 5 lines in your changes missing coverage. Please review.
⚠️ Please upload report for BASE (main@ac45bb2). Learn more about missing BASE report.

Files with missing lines Patch % Lines
...Valkey/Commands/Custom/GenericCustomCommands.swift 0.00% 3 Missing ⚠️
Sources/Valkey/Commands/GenericCommands.swift 0.00% 2 Missing ⚠️
Additional details and impacted files
@@           Coverage Diff           @@
##             main     #244   +/-   ##
=======================================
  Coverage        ?   39.73%           
=======================================
  Files           ?       94           
  Lines           ?    15774           
  Branches        ?        0           
=======================================
  Hits            ?     6268           
  Misses          ?     9506           
  Partials        ?        0           

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

public let keys: [ValkeyKey]

public init(fromRESP token: RESPToken) throws {
let (cursor, keys) = try token.decodeArrayElements(as: (Int, [ValkeyKey]).self)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can use (self.cursor, self.keys) = .... You don't need the temporary variables.

Copy link
Collaborator

@adam-fowler adam-fowler left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One minor change

@adam-fowler
Copy link
Collaborator

@natanrolnik I was thinking about this. In general I have not been converting RESPToken.Array to actual arrays in custom Responses as it involves additional allocations. RESPToken.Array is a sequence so it is iterable. So I think we should ditch the KEYS response type and set keys parameter in the SCAN response to be a RESPToken.Array. Users can get the keys either via

paying for the extra allocation if they want by converting to an Array

let scanResponse = try await client.scan(cursor: 0)
let keys = try scanResponse.keys.decode(as: [ValkeyKey].self)

or they can avoid the array allocation with

let scanResponse = try await client.scan(cursor: 0)
for token in scanResponse.keys {
    let key = try ValkeyKey(respToken: token)
    ...
}

Signed-off-by: Natan Rolnik <[email protected]>
@natanrolnik
Copy link
Contributor Author

@adam-fowler got it - done ✅

I was about to write that we needed more docs on RESPToken usage, then I saw #245 🙂

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants